package com.ncamc.pms.core.job;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.ncamc.pms.core.common.vo.DispatchJobRunReqVO;
import com.ncamc.pms.core.service.DispatchJobInfoService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CommonJob {

    public static final String name = "demoJobHandler1";
    @Autowired
    private DispatchJobInfoService dispatchJobInfoService;

    @XxlJob(name)
    public void demoJobHandler1() {
        try {
            // 调度参数
            String param = XxlJobHelper.getJobParam();
            log.info("调度 {} 开始执行, kettleCode={}", name, param);
            if (StrUtil.isNotBlank(param)) {
                dispatchJobInfoService.runJob(JSON.parseObject(param, DispatchJobRunReqVO.class));
            }
            XxlJobHelper.handleSuccess();
        } catch (Exception e) {
            log.error("调度 {} 执行失败", name, e);
            XxlJobHelper.handleFail(String.format("调度 %s 执行失败, %s", name, e.getMessage()));
        }
    }
}
